﻿using ShareCode.Entity.User;
using Infrastructure.Attributes.Ioc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShareCode.Dto;
using SqlSugar;
using System.Linq.Expressions;
using ShareCode.Dto.User.Org.Admin;
using ShareCode.Util.Extension;

namespace Dao.User
{
    /// <summary>
    /// by zhouxin
    ///</summary>
    [Component]
    public class UserOrgDao : BaseTreeDao<UserOrgEntity>
    {
        public override Expressionable<UserOrgEntity> GetSearchExpression(PagingDto pars)
        {
            var whereExpression = base.GetSearchExpression(pars);

            whereExpression = whereExpression.AndIF(pars.SearchField.HasString("Domain"), t => t.Domain == pars.SearchField.GetString("Domain"));


            return whereExpression;
        }


        public async override Task<PagingWithDataModel<UserOrgEntity>> ListPageAsync(PagingDto? pars)
        {
            pars ??= new PagingDto();

            var myReturn = new PagingWithDataModel<UserOrgEntity>(pars);


            if (pars.SearchField.ContainsKey("ContainSon") && pars.SearchField["ContainSon"] != "0") myReturn.PageSize = int.MaxValue;
            var page = this.TranslatePageModel(myReturn);


            Expression<Func<UserOrgEntity, bool>> whereExpression = Expressionable.Create<UserOrgEntity>()
                //.And(t => t.IsDelete == dto.IsDelete)
                //.AndIF(pars.SearchField.ContainsKey("TheCode"), t => t.TheCode.Contains(pars.SearchField["DoMain"]))
                .AndIF(pars.SearchField.ContainsKey("Title"), t => (t.Title ?? "").Contains(pars.SearchField["DoMain"]))
                //.AndIF(dto.ContainSon == 0, t => t.Floor == 0)
                .ToExpression();


            var list = await this.GetPageListAsync(whereExpression, page, t => t.Id, SqlSugar.OrderByType.Desc);

            myReturn.List = list;
            myReturn.TotalRows = page.TotalCount;

            return myReturn;
        }

        public async Task<PagingWithDataModel<AdminListDto>?> AdminListAsync(PagingDto? pars)
        {
            try
            {
                var myReturn = new PagingWithDataModel<AdminListDto>(pars);

                var page = this.TranslatePageModel(myReturn);

                RefAsync<int> TotalCount = page.TotalCount;

                Expression<Func<UserOrgEntity, UserOrgCountEntity, bool>> whereExpression = Expressionable.Create<UserOrgEntity, UserOrgCountEntity>()
                    .AndIF(pars?.IsDesc ?? false, (o, e) => o.Id == e.OrgId && o.Id < myReturn.LastId)
                    .AndIF(!(pars?.IsDesc ?? true), (o, e) => o.Id == e.OrgId && o.Id > myReturn.LastId)
                    .ToExpression();

                var list = await Context.Queryable<UserOrgEntity>()
                    .InnerJoin<UserOrgCountEntity>(whereExpression)
                    .OrderByIF(pars?.IsDesc ?? false, (o) => o.Id, OrderByType.Desc)
                    .OrderByIF(!(pars?.IsDesc ?? true), (o) => o.Id)
                    .Select((o, e) => new AdminListDto { OrgEntity = o, OrgCountEntity = e })
                    .ToPageListAsync(page.PageIndex, page.PageSize, TotalCount);

                myReturn.List = list;
                myReturn.TotalRows = TotalCount;
                if (list != default && list.Count > 0)
                {
                    myReturn.LastId = list[^1].OrgEntity?.Id ?? 0;
                }

                return myReturn;
            }
            catch //(Exception ex)
            {
                // var asdfasf = 1;
            }

            return null;
        }

    }
}
